﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;

public class InputFloorComponent
{
    bool _bPress = false;

    public void OnPointerDown(PointerEventData eventData)
    {
        //Debug.Log("InputFloorComponent.OnPointerDown ");

        _bPress = true;

        if (ZoneUI._clickOB != null)
        {
            ZoneUI._clickOB.Unload();
            ZoneUI._clickOB = null;
        }

        if (ZoneUI._clickOB == null)
        {
            GetObject(ZoneManager.Instance.floor);

            MoveClickOB();

        }
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        //Debug.Log("InputFloorComponent.OnPointerUp ");

        if (_bPress)
        {
            int order = MoveClickOB();

            if (_bPress)
            {
                AddObject(order, ZoneManager.Instance.floor);
            }
        }

        _bPress = false;
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (ZoneUI._clickOB != null)
        {
            int order = MoveClickOB();

            if (_bPress)
            {
                AddObject(order, ZoneManager.Instance.floor);
            }
        }
    }

    /// <summary>
    /// 获取一个虚拟对象
    /// </summary>
    /// <param name="name"></param>
    void GetObject(string name)
    {
        Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        pos.z = 0;

        nUnitData ud = ZoneManager.Instance._ZoneMediaManager.GetDataByID(int.Parse(name));
        ZoneUI._clickOB = ZoneManager.Instance._ZoneFactory.AddBuidingObject(ud);
        //ZoneUI._clickOB._Object.transform.position = pos;

        ZoneManager.Instance.VirualObject(ZoneUI._clickOB);
    }

    /// <summary>
    /// 移动虚拟对象
    /// </summary>
    int MoveClickOB()
    {
        UnitObject uo = (UnitObject)ZoneUI._clickOB;

        Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        pos.z = 0;

        uo.SetUnitRhombusLeft4World(pos, -1);

        pos = uo.GetUnitRhombusLeft4World();

        int order = ZoneManager._BuildingLayer.BlockFresh.GetClosePointOrder(pos, uo._VBOdata);

        return order;
    }

    /// <summary>
    /// 增加对象
    /// </summary>
    /// <param name="order"></param>
    /// <param name="name"></param>
    void AddObject(int order, string name)
    {
        nUnitData ud = ZoneManager.Instance._ZoneMediaManager.GetDataByID(int.Parse(name));
        if (ud._res_name.Contains("rhombus"))
        {
            if (!ud._res_name.Contains("rhombus04"))
            {
                if (ZoneManager._BuildingLayer.BlockFresh.IsCanDone(order, ZoneUI._clickOB._VBOdata))
                {
                    Vector3 pos = ZoneManager._BuildingLayer.BlockFresh._rgc._grids[order];

                    UnitObject uo = ZoneManager.Instance._ZoneFactory.AddBuidingObject(ud);
                    uo._unitData._Pos = pos;
                    uo._EditorName = name + "_" + order;
                    uo._GroudOrder = order;

                    ZoneManager.PushZoneBuilding(uo);
                }
            }
            else
            {
                ZoneManager.PopZoneBuilding(order);
            }

        }
    }
}
